<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="姓" field="surname">
            <a-input v-model="searchForm.surname" placeholder="请输入姓" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="名" field="name">
            <a-input v-model="searchForm.name" placeholder="请输入名" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../../api/meetingSpeakerPassport/passport'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  surname: '',
  name: '',
})

const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/cms/meetingSpeakerPassport/ProjectMeetingSpeakerPassport/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/cms/meetingSpeakerPassport/ProjectMeetingSpeakerPassport/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/cms/meetingSpeakerPassport/ProjectMeetingSpeakerPassport/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/cms/meetingSpeakerPassport/ProjectMeetingSpeakerPassport/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/cms/meetingSpeakerPassport/ProjectMeetingSpeakerPassport/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: 'id', dataIndex: 'id', width: 180 },
  { title: '受邀讲者表主键id  （project_meeting_invitation_speaker_id）', dataIndex: 'invitation_speaker_id', width: 180 },
  { title: '讲者id', dataIndex: 'speaker_id', width: 180 },
  { title: '状态', dataIndex: 'status', width: 180 },
  { title: '前端是否显示', dataIndex: 'is_display', width: 180 },
  { title: '汇款信息', dataIndex: 'remittance', width: 180 },
  { title: '姓', dataIndex: 'surname', width: 180 },
  { title: '名', dataIndex: 'name', width: 180 },
  { title: '出生日期', dataIndex: 'birth', width: 180 },
  { title: '护照号', dataIndex: 'passport_number', width: 180 },
  { title: '国籍', dataIndex: 'nationality', width: 180 },
  { title: '性别', dataIndex: 'sex', width: 180 },
  { title: '出生地', dataIndex: 'place_of_birth', width: 180 },
  { title: '护照签发地', dataIndex: 'passport_issuing_place', width: 180 },
  { title: '有效期', dataIndex: 'validity', width: 180 },
  { title: '上传护照url', dataIndex: 'upload_passport', width: 180 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
